#!/bin/gawk -f
# Summarize NUMBER_OF_RESTRAINTS_per_RESIDUE from .nrc/.hrc/.src file
# Arguments: nresid: number of residues
#            nrestr: number of restraints
{
    for (i=4;i<=7;i++) {
	t[i]+=$i
	t2[i]+=$i*$i
    }
    cnt++
}
END{
    if (cnt!=nresid) {
	print "ERROR"
	exit(1)
    }
    printf("Number of restraints:  %i\n", nrestr)
    printf("Number of residues:    %i\n", nresid)
    printf("Overall ratio:         %.1f restraints/residue\n", nrestr/nresid)
    print "# restraints per residue:  average    rms  "
    for (i=4;i<=7;i++) {
	av[i]=t[i]/cnt
	r[i]=(t2[i]/cnt)-(av[i]*av[i])
	if (r[i]<0.) r[i]=0.
	r[i]=sqrt(r[i])
	if (i==4) printf("    only lower bound present  ")
	if (i==5) printf("    only upper bound present  ")
	if (i==6) printf("    both bounds present       ")
	if (i==7) printf("    total                     ")
	printf("%4.1f   %4.1f\n",av[i],r[i])
    }
    print "  Note: this table is based on counting the number of restraints"
    print "  involving a particular residue. The average differs from the"
    print "  overall ratio because inter-residue restraints are counted doubly."
}
